const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            policy: [],
            land: [],
            record: [],
            rules: {
                Party_Organization_Name: [
                    { required: true, message: "党组织名称不能为空", trigger: "blur" },
                    { min: 2, max: 50, message: "长度在2-50个字符", trigger: "blur" },
                ],
                Establishment_Date: [
                    { required: true, message: "请选择成立日期", trigger: "change" },
                ],
                Party_Organization_Type: [
                    { required: true, message: "请选择组织类型", trigger: "change" },
                ],
                Party_Organization_Level: [
                    { required: true, message: "请选择党组级别", trigger: "change" },
                ],
                Party_Organization_Address: [
                    { required: true, message: "请输入党组地址", trigger: "blur" },
                    { min: 5, max: 100, message: "长度在5-100个字符", trigger: "blur" },
                ],
                Party_Organization_Captain_Number: [
                    { required: true, message: "请输入负责人编号", trigger: "blur" },
                ],
            },
            value: [], // 初始化 labelNumber 属性
            hjoptions: [], //省市区县
            Nation: [], //民族
            Type: [
                {
                    value: "党委",
                    label: "党委",
                },
                {
                    value: "总支",
                    label: "总支",
                },
                {
                    value: "支部",
                    label: "支部",
                },
            ],
            Manager: [], //上级党组
            Level: [
                //党组级别
                {
                    value: "省",
                    label: "省",
                },
                {
                    value: "市",
                    label: "市",
                },
                {
                    value: "区/县",
                    label: "区/县",
                },
                {
                    value: "乡镇/街道",
                    label: "乡镇/街道",
                },
                {
                    value: "村",
                    label: "村",
                },
            ],
            Enterprise: [], //党组所在企业
            showDialog: false,
            fileUrl: '',
        };
    },
    mounted() {
        this.getCategory();
    },
    watch: {
        'dataForm.Policy_SerialNumber'(val) {
            if (val) {
                $.ajax({
                    url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                    type: "POST",
                    data: {
                        SQLSerialNumber: "f_subsidy_policy_detail",
                        Condition: window.btoa(unescape(encodeURIComponent(val))),
                    },
                    success: (data) => {
                        this.record = data;
                    },
                    dataType: "json",
                });
            }
        }
    },
    methods: {
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'party_organization',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    console.log(data);
                    this.Manager = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'subsidy_policy',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.policy = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'government_agencies',
                    Condition: `Row_Status='0'`,
                },
                success: (res) => {
                    this.Enterprise = res;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        // changeRecord(val){
        //     $.ajax({
        //         url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        //         type: "POST",
        //         data: {
        //             SQLSerialNumber: "f_subsidy_policy_detail",
        //             Condition: window.btoa(unescape(encodeURIComponent(val))),
        //         },
        //         success: (data) => {
        //             this.record = data;
        //         },
        //         dataType: "json",
        //     });
        // },
        changeRecord(val) {
            if (val) {
                this.dataForm.Land_SerialNumber = this.record.filter(i => i.Record_SerialNumber == val)[0].Land_SerialNumber
            } else {
                this.dataForm.Land_SerialNumber = '';
            }
        },
        handleSubmit() {
            let ls_URL = window.location.href;
            const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
            let lJSONObject_Data_Options = new Object();
            lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

            let ls_MainTableName = lJSONObject_Data_Options.TableName;
            let ls_DataForm = lJSONObject_Data_Options.form;
            //没有子表下面内容就会报错，try/catch抛出异常
            try {
                //可能涉及多个子表
                document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                    let lJSONObject_ChildData_Options = new Object();
                    lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                    let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                    let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                    // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
                    //建立子表结构体
                    let JSONArray_ChildRecord = new Array();
                    JSONArray_ChildRecord = this[ls_ChildDataForm];
                    if (!this[ls_DataForm].Children) {
                        this[ls_DataForm].Children = new Object();
                    }
                    this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                })
            } catch (error) {

            }
            //建立新增提交结构
            let lJSONObject_DataSet = new Object();
            if (action == 'Add') {
                if (this.nodeKey) {
                    this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                }
                this[ls_DataForm].News_Publish_Datetime = getYMDHMSDate();
                let lJSONArray_Record = new Array();
                lJSONArray_Record.push(this[ls_DataForm]);
                let lJSONObject_Table = new Object();
                lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                lJSONObject_DataSet[action] = lJSONObject_Table;
            } else if (action == 'Modify') {
                const { add, modify } = this.deal(this[ls_DataForm]);
                let lJSONArray_Record = new Array();
                lJSONArray_Record.push(modify);

                let lJSONObject_Table = new Object();
                lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

                lJSONObject_DataSet[action] = lJSONObject_Table;
                if (add != null) {
                    let lJSONArray_Record_Add = new Array();
                    lJSONArray_Record_Add.push(add);

                    let lJSONObject_Table_Add = new Object();
                    lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

                    lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
                }
            }
            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
            if (!lJSONObject_DataSet['Add']) {
                lJSONObject_DataSet['Add'] = new Object();
            }
            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                Operation_Type: '0',
                Operation_Path: this.relativePath,
                Operation_Main,
                Operation_Interface: "generalSave",
                Operation_IP: getIpAddress()
            }]
            submitSave(lJSONObject_DataSet,(res)=>{
                if (res.Status == 'success') {
                    this.$confirm('是否跳转到换届补选前往配置, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        layui.use(['jquery', 'pear'], function () {
                            const pear = layui.pear;
                            // 调用 Pear.addTab 打开目标页面
                            pear.addTab({
                                id: 'BSML00000000038', // 菜单 ID
                                title: '换届、补选', // 选项卡标题
                                url: 'view/party_organization_tenure/index.html', // 页面路径
                                closeBtn: true // 显示关闭按钮
                            });
                        });
                        // 等待 3 秒后回退页面
                        setTimeout(() => {
                            window.history.back();
                        }, 1000);
                    }).catch(() => {
                        window.location.href = './index.html'
                    });

                }
            })
            // $.ajax({
            //     headers: requestHeaders(),
            //     url: `${mh}/generalBusiness/generalSave`,
            //     method: "post",
            //     data: {
            //         Data: JSON.stringify(lJSONObject_DataSet),
            //     },
            //     success: (res) => {
            //         if (res.Status == 'success') {
            //             this.$confirm('是否跳转到换届补选前往配置, 是否继续?', '提示', {
            //                 confirmButtonText: '确定',
            //                 cancelButtonText: '取消',
            //                 type: 'warning'
            //             }).then(() => {
            //                 layui.use(['jquery', 'pear'], function () {
            //                     const pear = layui.pear;
            //                     // 调用 Pear.addTab 打开目标页面
            //                     pear.addTab({
            //                         id: 'BSML00000000038', // 菜单 ID
            //                         title: '换届、补选', // 选项卡标题
            //                         url: 'view/party_organization_tenure/index.html', // 页面路径
            //                         closeBtn: true // 显示关闭按钮
            //                     });
            //                 });
            //                 // 等待 3 秒后回退页面
            //                 setTimeout(() => {
            //                     window.history.back();
            //                 }, 1000);
            //             }).catch(() => {
            //                 window.location.href = './index.html'
            //             });

            //         }
            //     },
            //     complete: (XMLHttpRequest) => {
            //         requestPretreatment(XMLHttpRequest);
            //     },
            // });
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
